/*
<<<<<<< .mine
 * File Name��ProgressCircleMediaPlay.java
 * Copyright��Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description�� ProgressCircleMediaPlay.java
 * Modify By��res-mingyang
 * Modify Date��2015-7-24
 * Modify Type��Add
=======
 * File Name：ProgressCircleMediaPlay.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： ProgressCircleMediaPlay.java
 * Modify By：res-mingyang
 * Modify Date：2015-7-24
 * Modify Type：Add
>>>>>>> .r102
 */
package com.ciwong.mobilelib.widget.progressBar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.ciwong.mobilelib.R;

/**
<<<<<<< .mine
 * TODO Description of class��
=======
 * TODO Description of class。
>>>>>>> .r102
 * <p>
 * TODO Detail Description
 * <p>
 * TODO Sample Code
 *
 * <pre>
 * </pre>
 *
 * @author res-mingyang
 * @version ciwong v.1.0 2015-7-24
 * @since ciwong v.1.0
 */
public class ProgressCircleMediaPlay extends ImageView
{
    private Context mContext;

    private int RADIU;

    private final int DURATION = 3000;

    private final float PERIMETER = 0.9f;

    private final int STATU_NORNAL = 0;

    private final int STATU_LOADING = 1;

    private final int STATU_DOWNLOADING = 2;

    private int mCurrentStatu;

    private Paint paint;

    private float progress;

    private int degree;

    private Bitmap centerBitmap;

    public ProgressCircleMediaPlay(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public ProgressCircleMediaPlay(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public ProgressCircleMediaPlay(Context context)
    {
        this(context, null);
    }

    private void init()
    {
        this.mCurrentStatu = STATU_LOADING;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
        centerBitmap = BitmapFactory.decodeResource(getResources(),
                R.mipmap.player);
        updateDownloadProgress(0);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        int centerX = getMeasuredWidth() / 2;
        int centerY = getMeasuredHeight() / 2;
        if (RADIU == 0)
        {
            this.RADIU = (int) (getMeasuredHeight() * 0.4f);
        }
        super.onDraw(canvas);
        RectF rectF = new RectF();
        rectF.left = centerX - RADIU;
        rectF.top = centerY - RADIU;
        rectF.right = centerX + RADIU;
        rectF.bottom = centerY + RADIU;
        switch (mCurrentStatu)
        {
            case STATU_NORNAL:
                // Do nothing
                break;
            case STATU_LOADING:
                break;
            case STATU_DOWNLOADING:
                paint.setColor(Color.parseColor("#77333333"));
                canvas.drawCircle(centerX, centerY, centerX, paint);
                paint.setColor(Color.parseColor("#FFA523"));
                canvas.drawArc(rectF, -90, 360 * progress, true, paint);
                paint.setColor(Color.parseColor("#36322D"));
                canvas.drawCircle(centerX, centerY, RADIU - 10, paint);
                drawCenterBitmap(canvas, centerX, centerY);
                break;
        }
    }

    private void drawCenterBitmap(Canvas canvas, int centerX, int centerY)
    {
        Rect src = new Rect();// 图片
        Rect dst = new Rect();// 屏幕位置及尺寸
        // src 这个是表示绘画图片的大小
        src.left = 0; // 0,0
        src.top = 0;
        src.right = centerBitmap.getWidth();
        src.bottom = centerBitmap.getHeight();
        // 下面的 dst 是表示 绘画这个图片的位置
        dst.left = centerX - centerX * 2 / 8 - centerX / 16;
        dst.top = centerY - centerX * 3 / 8;

        dst.right = centerX + centerX / 2 - centerX / 16;
        dst.bottom = centerY + centerX * 3 / 8;
        canvas.drawBitmap(centerBitmap, src, dst, null);
        src = null;
        dst = null;
    }

    public void updateDownloadProgress(float progress)
    {
        this.progress = progress;
        this.mCurrentStatu = STATU_DOWNLOADING;
        invalidate();
    }

}
